package com.wxcms.ctrl;

import com.wxcms.domain.AccountFans;
import com.wxcms.domain.EimpAccount;
import com.wxcms.domain.Org;
import com.wxcms.service.AccountFansService;
import com.wxcms.service.EimpAccountService;
import com.wxcms.service.EimpAccountService;
import com.wxcms.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 
 */

@Controller
@RequestMapping("/wxcms/eimpaccount")
public class EimpAccountCtrl {

	@Autowired
	private EimpAccountService entityService;
	@Autowired
	private AccountFansService fansService;
	
	@RequestMapping(value = "/getById")
	public ModelAndView getById(String id){
		entityService.getById(id);
		return new ModelAndView();
	}

	@RequestMapping(value = "/list")
	public  ModelAndView list(@ModelAttribute EimpAccount searchEntity){
		ModelAndView modelAndView = new ModelAndView("wxcms/eimpAccountList");
		List<EimpAccount> list = entityService.listForPage(searchEntity);
		modelAndView.addObject("pageList", list);
		modelAndView.addObject("cur_nav", "eimpAccount");
		return modelAndView;
	}
	
	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(EimpAccount entity){
		ModelAndView modelAndView = new ModelAndView("wxcms/eimpAccountMerge");
		EimpAccount eimpAccount = new EimpAccount();
		if(entity.getId()!=null){
			eimpAccount = entityService.getById(entity.getId().toString());
		}
		else {
			AccountFans fans = fansService.getByOpenId(entity.getOpenid());
			eimpAccount.setNicknameStr(fans.getNicknameStr());
			eimpAccount.setOpenid(fans.getOpenId());
		}
		modelAndView.addObject("entity", eimpAccount);
		return modelAndView;
	}

	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(EimpAccount entity){
		if(entity.getId()!=null){
			entityService.update(entity);
		}
		else {
			entityService.add(entity);
		}
		return new ModelAndView("redirect:/wxcms/eimpaccount/list.html");
	}

	@RequestMapping(value = "/delete")
	public ModelAndView delete(EimpAccount entity){
		ModelAndView modelAndView = new ModelAndView("redirect:/wxcms/eimpaccount/list.html");
		entityService.delete(entity);
		return modelAndView;
	}
	
}
